package com.jy.api.controller.StockProductController;

import com.jy.api.dao.StockInfoDao;
import com.jy.api.dao.StockOrderItemDao;
import com.jy.api.dao.StockOrderShopDao;
import com.jy.api.dao.StoreDao;
import com.jy.api.domain.*;
import com.jy.api.enums.PayTypeEnum;
import com.jy.api.service.*;
import com.jy.api.signUpReq.MarsCallReq.CallEmpRes;
import com.jy.api.signUpReq.StockInfoReq;
import com.jy.api.signUpReq.StockOrderShopReq;
import com.jy.api.signUpReq.StockProductReq;
import com.jy.api.signUpReq.StoreOrderSignUpReq;
import com.jy.api.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaohua on 2019/4/19.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/stock", produces = "application/json;charset=utf-8")
public class StockProductController {


    @Autowired
    private StockProductTypeService typeService;
    @Autowired
    private StockSpecService specService;
    @Autowired
    private StockProductService productService;
    @Autowired
    private StockOrderShopService shopService;
    @Autowired
    private StockInfoDao stockInfoDao;
    @Autowired
    private StockOrderShopDao shopDao;
    @Autowired
    private StockInfoService infoService;
    @Autowired
    private PaymentService paymentService;
    @Autowired
    private StockOrderItemDao itemDao;
    @Autowired
    private StoreDao storeDao;

    @GetMapping("type-list")
    public ResponseEntity typeFindAll() {
        return ResponseEntity.ok(typeService.findAll());
    }

    @GetMapping("type-merge")
    public ResponseEntity typeMerge(@RequestParam(value = "id", required = false) int id, @RequestParam("name") String name) {
        typeService.addOrUpdateProduct(name, id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("type-remove")
    public ResponseEntity typeRemove(@RequestParam("id") int id) {
        return ResponseEntity.ok(typeService.deleteProduct(id));
    }

    @GetMapping("spec-list")
    public ResponseEntity specFindAll() {
        return ResponseEntity.ok(specService.findAll());
    }

    @GetMapping("spec-merge")
    public ResponseEntity specMerge(@RequestParam(value = "id", required = false) int id, @RequestParam("name") String name) {
        specService.updateOrSave(name, id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("spec-remove")
    public ResponseEntity specRemove(@RequestParam("id") int id) {
        return ResponseEntity.ok(specService.deleteById(id));
    }

    @GetMapping("pro-list")
    public ResponseEntity proFindAll(@RequestParam(value = "type", required = false) int typeId) {
        return ResponseEntity.ok(productService.findByTypeId(typeId));
    }

    @PostMapping("pro-merge")
    public ResponseEntity proMerge(@RequestBody StockProductReq req) {
        productService.updateOrSave(req);
        return ResponseEntity.ok().build();
    }

    @GetMapping("pro-remove")
    public ResponseEntity proRemove(@RequestParam("id") int id) {
        productService.deleteById(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("shop-list")
    public ResponseEntity shopList(@RequestParam("userName") String userName, Pageable pageable) {
        return ResponseEntity.ok(shopService.findAllByPage(userName, pageable));
    }

    @PostMapping("shop-merge")
    public ResponseEntity shopMerge(@RequestBody StockOrderShopReq req) {
        shopService.shopMerge(req);
        return ResponseEntity.ok().build();
    }

    @PostMapping("shop-order")
    public ResponseEntity shopOrder(@RequestBody StoreOrderSignUpReq req) {
        return ResponseEntity.ok(shopService.orderShop(req));
    }

    @GetMapping("info-list")
    public ResponseEntity infoList(@RequestParam("userName") String userName) {
        return ResponseEntity.ok(stockInfoDao.findByUserNameAndEnabled(userName, true));
    }

    @PostMapping("info-merge")
    public ResponseEntity infoMerge(@RequestBody StockInfoReq req) {
        infoService.saveInfoOrUpdate(req);
        return ResponseEntity.ok().build();
    }

    @PostMapping("delete-order")
    public ResponseEntity deleteOrder(@RequestBody StockOrderShopReq req) {
        StockOrder order = shopDao.findOne(req.getId());
        order.setEnabled(false);
        shopDao.save(order);
        return ResponseEntity.ok().build();
    }


    @PostMapping("info-remove")
    public ResponseEntity infoRemove(@RequestBody StockInfoReq req) {
        infoService.deleteInfoById(req);
        return ResponseEntity.ok().build();
    }

   /* @PostMapping("pay")
    public ResponseEntity pay(@RequestBody CallEmpRes res) {
        if (res.getSource().equals("UNPAY")) {
            StockOrder order = shopDao.findOne(res.getOrderId());
            order.setStatus("UNPAY");
            shopDao.save(order);
            return ResponseEntity.ok().build();
        } else {
        *//*    PayTypeEnum payType = res.isType() ? PayTypeEnum.PAY_ALIPAY_NATIVE : PayTypeEnum.PAY_WX_NATIVE;
            SwiftPassQRCodePayResult result = paymentService.qrStockOrderCodePay(res.getOrderId(), payType);
            Map<String, String> map = new HashMap();
            map.put("payMode", "THIRD_PARTY");
            map.put("url", result.getCodeImgURL());
            if (result.isSuccess()) {
                return ResponseEntity.ok(map);
            } else {
                return ResponseEntity.badRequest().body(result.getErrMsg());
            }*//*
        }
    }
*/
    @GetMapping("order-one")
    public ResponseEntity one(@RequestParam("orderId") String id) {
        StockOrder order = shopDao.findOne(id);
        if (order != null) {
            if (order.isPayEnabled()) {
                return ResponseEntity.ok(new Result(200, "支付成功！"));
            } else {
                return ResponseEntity.ok(new Result(201, "支付中，请等待！"));
            }
        } else {
            return ResponseEntity.ok(new Result(400, "订单不存在，请核实!"));
        }
    }

    @GetMapping("item-merge")
    public ResponseEntity itemMerge(@RequestParam("id") int id, @RequestParam("enabled") boolean enabled) {
        List<StockOrderItem> list = new ArrayList<>();
        StockOrderItem item = itemDao.findOne(id);
        item.setSendEnabled(enabled);
        StockOrderItem save = itemDao.save(item);
        list.add(save);
        StockOrder order = shopDao.findByItems(list);
        return ResponseEntity.ok(order);
    }

    @PostMapping("pur-merge")
    public ResponseEntity purchaceMerge(@RequestBody StockInfoReq req) {
        Store store = storeDao.findOne(req.getStoreId());
        store.setPurchaseEnabled(req.isPurchaseEnabled());
        storeDao.save(store);
        return ResponseEntity.ok().build();
    }


}
